<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use App\Helpers\SuccessResponse;
use App\Helpers\ValidatorResponse;
use Ixudra\Curl\Facades\Curl;
use ElasticSearch\Client;
// set_time_limit(0);
class EsController extends Controller{
	private $_es;

	public function __construct(){
		$es_host = env('ES_HOST', '');
		$es_index = env('ES_INDEX', '');
		$this->_es = Client::connection(array(
		    'servers' => $es_host,
		    'protocol' => 'http',
		    'index' => $es_index,
		));
	}

	public function jobList(Request $request){
		$param_cursor = intval($request->input('cursor'));
		$param_cursor = $param_cursor > 0 ? $param_cursor : 0;
		$param_size = intval($request->input('size'));
		$param_size = $param_size > 0 ? $param_size : 10;

		$param_salary_from = intval($request->input('salary_from'));
		$param_salary_from = $param_salary_from > 0 ? $param_salary_from : 0;
		$es_must = [[
			'range' => [
				'salary_range_from' => [
					'from' => $param_salary_from,
					'to' => null,
					'include_lower' => true,
					'include_upper' => true,
				],
			],
		]];

		$param_degree = trim(strval($request->input('degree')));
		$param_degree = strlen($param_degree) > 0 ? $param_degree : false;
		if($param_degree){
			array_push($es_must, [
				'match' => [
					'degree_key' => [
						'query' => $param_degree,
						'type' => 'phrase'
					],
				]
			]);
		}

		$param_company_size = trim(strval($request->input('company_size')));
		$param_company_size = strlen($param_company_size) > 0 ? $param_company_size : false;
		if($param_company_size){
			array_push($es_must, [
				'match' => [
					'company_size_key' => [
						'query' => $param_company_size,
						'type' => 'phrase'
					],
				]
			]);
		}

		$param_xp_lvl = trim(strval($request->input('xp_lvl')));
		$param_xp_lvl = strlen($param_xp_lvl) > 0 ? $param_xp_lvl : false;
		if($param_xp_lvl){
			array_push($es_must, [
				'match' => [
					'xp_lvl_key' => [
						'query' => $param_xp_lvl,
						'type' => 'phrase'
					],
				]
			]);
		}

		$param_recruiter_verify = intval($request->input('recruiter_verify'));
		$param_recruiter_verify = $param_recruiter_verify > 0 ? $param_recruiter_verify : false;
		if($param_recruiter_verify){
			array_push($es_must, [
				'match' => [
					'is_recruiter_verify' => [
						'query' => true,
						'type' => 'phrase'
					],
				]
			]);
		}

		$param_job_location = trim(strval($request->input('job_location')));
		$param_job_location = strlen($param_job_location) > 0 ? $param_job_location : false;
		if($param_job_location){
			array_push($es_must, [
				'match' => [
					'job_location_key' => [
						'query' => $param_job_location,
						'type' => 'phrase'
					],
				]
			]);
		}


		$param_sort = intval($request->input('sort'));
		$param_sort = in_array($param_sort, [2,3]) ? $param_sort : 2;
		if($param_sort == 3){
			$es_sort = [
				[
					'salary_range_to' => [
						'order' => 'desc'
					]
				]
			];
		}else{
			$es_sort = [
				[
					'id' => [
						'order' => 'desc'
					]
				]
			];
		}

		$elastic_search = [
			'from' => $param_cursor,
			'size' => $param_size,
			'query' => [
				'bool' => [
					'must' => [
						'bool' => [
							'must' => $es_must,
						],
					],
				],
			],
			'sort' => $es_sort,
		];
		return $this->_es->setType('job')->search($elastic_search);
	}

	public function seekerList(Request $request){
		$param_cursor = intval($request->input('cursor'));
		$param_cursor = $param_cursor > 0 ? $param_cursor : 0;
		$param_size = intval($request->input('size'));
		$param_size = $param_size > 0 ? $param_size : 10;

		$param_salary_from = intval($request->input('salary_from'));
		$param_salary_from = $param_salary_from > 0 ? $param_salary_from : 0;
		$es_must = [[
			'range' => [
				'latest_preference.salary_range_from' => [
					'from' => $param_salary_from,
					'to' => null,
					'include_lower' => true,
					'include_upper' => true,
				],
			],
		]];

		$param_xp_lvl = trim(strval($request->input('xp_lvl')));
		$param_xp_lvl = strlen($param_xp_lvl) > 0 ? $param_xp_lvl : false;
		if($param_xp_lvl){
			array_push($es_must, [
				'match' => [
					'xp_lvl_key' => [
						'query' => $param_xp_lvl,
						'type' => 'phrase'
					],
				]
			]);
		}

		$param_employment_status = trim(strval($request->input('employment_status')));
		$param_employment_status = strlen($param_employment_status) > 0 ? $param_employment_status : false;
		if($param_employment_status){
			array_push($es_must, [
				'match' => [
					'employment_status_key' => [
						'query' => $param_employment_status,
						'type' => 'phrase'
					],
				]
			]);
		}

		$param_degree = trim(strval($request->input('degree')));
		$param_degree = strlen($param_degree) > 0 ? $param_degree : false;
		if($param_degree){
			array_push($es_must, [
				'match' => [
					'latest_education.degree_key' => [
						'query' => $param_degree,
						'type' => 'phrase'
					],
				]
			]);
		}
		
		$elastic_search = [
			'from' => $param_cursor,
			'size' => $param_size,
			'query' => [
				'bool' => [
					'must' => [
						'bool' => [
							'must' => $es_must,
						],
					],
				],
			],
			'sort' => [
				[
					'id' => [
						'order' => 'desc'
					]
				]
			]
		];
		return $this->_es->setType('seeker')->search($elastic_search);
	}

	// private function _createTestSeekerEsData(){
	// 	$get_user_base_url = 'http://development-data-bossjob.3nvamzxn9j.ap-southeast-1.elasticbeanstalk.com/api/v1/users/';
	// 	$res = [];
	// 	for($i=1; $i<200; $i++){
	// 		$url = $get_user_base_url.$i;
	// 		$response = Curl::to($url)->get();
	// 		$response = json_decode($response, true);
	// 		$user = $response['data'];
	// 		if($user){
	// 			$es_res = $this->_syncUserToElasticSearch($user);
	// 			array_push($res, $es_res);
	// 		}
	// 	}
	// 	return $res;
	// }

	// private function _syncUserToElasticSearch($user){
	// 	$es_type = 'seeker';
	// 	$es_post_url = implode([env('ES_HOST', ''), env('ES_INDEX', ''), $es_type, ''], '/');
	// 	$response = Curl::to($es_post_url)->withData(json_encode($user))->post();

	// 	$response = json_decode($response, true);
	// 	return $response;
	// }

	// private function _syncJobToElasticSearch($job){
	// 	$es_type = 'job';
	// 	$es_post_url = implode([env('ES_HOST', ''), env('ES_INDEX', ''), $es_type, ''], '/');
	// 	$response = Curl::to($es_post_url)->withData(json_encode($job))->post();

	// 	$response = json_decode($response, true);
	// 	return $response;
	// }

	// private function _createTestJobEsData(){
	// 	$get_job_base_url = 'http://development-data-bossjob.3nvamzxn9j.ap-southeast-1.elasticbeanstalk.com/api/v1/jobs/';

	// 	$res = [];
	// 	for($i=201; $i<1000; $i++){
	// 		$url = $get_job_base_url.$i;
	// 		$response = Curl::to($url)->get();
	// 		$response = json_decode($response, true);
	// 		$job = $response['data'];
	// 		if($job){
	// 			$es_res = $this->_syncJobToElasticSearch($job);
	// 			array_push($res, $es_res);
	// 		}
	// 	}
	// 	return $res;
	// }
}

// select * from job where degree='aaa' and salary_from >= 1000 order by id desc limit 100,20
// {
// 	"from": 100,
// 	"size": 20,
// 	"query": {
// 		"bool": {
// 			"must": {
// 				"bool": {
// 					"must": [
// 						{
// 							"match": {
// 								"degree": {
// 									"query": "aaa",
// 									"type": "phrase"
// 								}
// 							}
// 						},
// 						{
// 							"range": {
// 								"salary_from": {
// 									"from": 1000,
// 									"to": null,
// 									"include_lower": true,
// 									"include_upper": true
// 								}
// 							}
// 						}
// 					]
// 				}
// 			}
// 		}
// 	},
// 	"sort": [
// 		{
// 			"id": {
// 				"order": "desc"
// 			}
// 		}
// 	]
// }

// select * from job where degree_key='aaa' and salary_range_from >= 1000 order by id desc limit 100,20

// {
// 	"from": 100,
// 	"size": 20,
// 	"query": {
// 		"bool": {
// 			"must": {
// 				"bool": {
// 					"must": [
// 						{
// 							"match": {
// 								"degree_key": {
// 									"query": "aaa",
// 									"type": "phrase"
// 								}
// 							}
// 						},
// 						{
// 							"range": {
// 								"salary_range_from": {
// 									"from": 1000,
// 									"to": null,
// 									"include_lower": true,
// 									"include_upper": true
// 								}
// 							}
// 						}
// 					]
// 				}
// 			}
// 		}
// 	},
// 	"sort": [
// 		{
// 			"id": {
// 				"order": "desc"
// 			}
// 		}
// 	]
// }